package com.xishu.response;

import com.xishu.exception.ResponseStatusException;
import com.xishu.util.ClassUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.Supplier;

public class VerifyUtil {
    private static Logger logger = LoggerFactory.getLogger(VerifyUtil.class);

    /**
     * 判断是否为空
     *
     * @param paramList
     */
    public static void isEmpty(String... paramList) throws ResponseStatusException {
        for (String s : paramList) {
            logger.info("param is {}", s);
            if (StringUtils.isEmpty(s)) {
                throwError(ResponseStatus.INVALID_PARAMETER);
            }
        }
    }

    /**
     * 判断是否为空
     *
     * @param paramList
     */
    public static void isEmpty(Object... paramList) throws ResponseStatusException {
        for (Object s : paramList) {
            logger.info("param is {}", s);
            if (s == null) {
                throwError(ResponseStatus.INVALID_PARAMETER);
            }
        }
    }


    /**
     * 校验
     *
     * @param test
     */
    public static void verify(Supplier<Boolean> test, ResponseStatus status) throws ResponseStatusException {
        if (!test.get()) {
            if (status == null) {
                throwError(ResponseStatus.INVALID_PARAMETER);
            } else {
                throwError(status);
            }
        }
    }

    /**
     * 带参数的异常值
     *
     * @param test
     * @param status
     * @param params
     */
    public static void verify(Supplier<Boolean> test, ResponseStatus status, Object... params) throws ResponseStatusException {
        if (!test.get()) {
            if (status == null) {
                throwError(ResponseStatus.INVALID_PARAMETER);
            } else if (params == null || params.length == 0) {
                throwError(status);
            } else {
                //带参数
                String msg = String.format(status.getMsg(), params);
                String msg_en = String.format(status.getMsg_en(), params);
                ThreadContext.put("verify.msg", msg);
                ThreadContext.put("verify.msg.en", msg_en);
                logger.info("will throw param response status {}, zh {} en {}", params, msg, msg_en);
                throwError(status);
            }
        }
    }

    /**
     * 带参数的异常值，并且有国际化
     *
     * @param test
     * @param status
     * @param paramsZh
     */
    public static void verifyZhEn(Supplier<Boolean> test, ResponseStatus status, Object[] paramsZh, Object[] paramsEn) throws ResponseStatusException {
        if (!test.get()) {
            if (status == null) {
                throwError(ResponseStatus.INVALID_PARAMETER);
            } else if (paramsZh == null || paramsZh.length == 0) {
                throwError(status);
            } else if (paramsEn == null || paramsEn.length == 0) {
                throwError(status);
            } else {
                //带参数
                String msg = String.format(status.getMsg(), paramsZh);
                String msg_en = String.format(status.getMsg_en(), paramsEn);
                ThreadContext.put("verify.msg", msg);
                ThreadContext.put("verify.msg.en", msg_en);
                logger.info("will throw param response status {}, zh {} en {}", paramsZh, msg, msg_en);
                throwError(status);
            }
        }
    }

    /**
     * 校验
     *
     * @param test
     */
    public static void verify(Supplier<Boolean> test) throws ResponseStatusException {
        verify(test, null);
    }


    public static void throwError(ResponseStatus responseStatus) throws ResponseStatusException {
        logger.info("status is {}", responseStatus.getStatus());
        throw new ResponseStatusException(responseStatus);
    }


}
